package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 西宁网络电视台  网站解析
 * @author Chase	
 *
 */
public class XntvNewsAnalyse implements AnalyseNews {
	
	private static final String[] regex={
		"http://news.xntv.tv/.*/[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}/[0-9]*.html"
	};
		
	
			
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String url=urlMeta.getUrl();	
		/*
		 * 网站源码
		 */
		String htmltxt=urlMeta.getHtml();
			
		if(!isDetailPage(url)){
		}				
		
		
		String title=null;
		String content=null;
		Long date=null;
		String author=null;
		Document doc=Jsoup.parse(htmltxt);
		
		/*
		 * 标题
		 */
		title=doc.select("div.bt").text();
		/*
		 * 时间
		 */
		date=DateUtils.matchDate(doc.select("div.sj").text());
		
		if(StringUtils.isBlank(title)){
		}
		
		/*
		 * 内容
		 */
		content=HtmlCleaner.getContentHtml(url,doc.select("div.jianjie"));
		author=JSoupUtils.matchAuthor(doc, "来源：");
		
		/*
		 * 链接
		 */
		news.setUrl(url);	
		news.setAuthor(author);
		/*
		 * 来源
		 */
		news.setTitle(title);
		news.setContent(content);
		news.setDate(date);
		return news;
	}
	
	public static void main(String[] args) {	
		XntvNewsAnalyse analyse=new XntvNewsAnalyse();
		String url="http://news.xntv.tv/am/q/2014-07-16/11799.html";
		url=UrlArgumentTop.FromatUrl(url);
		System.err.println(url);
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	

	
	public boolean isNeedUpdate(){
		return false;
	}
}
